package com.wm.wmim.connect.ws.manager;

import com.wm.wmim.common.constant.Constant;
import com.wm.wmim.connect.center.api.bo.req.RouteDeleteReqBO;
import com.wm.wmim.connect.center.api.bo.req.RouteGetReqBO;
import com.wm.wmim.connect.center.api.bo.req.RouteSaveReqBO;
import com.wm.wmim.connect.center.api.bo.resp.RouteGetRespBO;
import com.wm.wmim.connect.ws.rpcinvoke.RouteInfoRpcInvokeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;
import java.net.InetAddress;

/**
 * @author weiming
 * @date 2024/10/19 13:03
 * @description
 */
@Slf4j
@Service
public class WebSocketSessionRouteManager {

    @Resource
    private RouteInfoRpcInvokeService routeInfoRpcInvokeService;
    @Value("${dubbo.protocol.port}")
    private String dubboPort;

    public void saveRouteAndSession(WebSocketSession session)  {
        if (session == null){
            return;
        }
        // 保存路由
        String ip = this.getCurrentIP();
        Long userID = WebSocketSessionManager.getSessionAttribute(session, Constant.WsParamConstant.USER_ID, Long.class);
        String userCode = WebSocketSessionManager.getSessionAttribute(session, Constant.WsParamConstant.USER_CODE, String.class);
        String businessScene = WebSocketSessionManager.getSessionAttribute(session, Constant.WsParamConstant.BUSINESS_SCENE, String.class);
        RouteSaveReqBO routeSaveReqBO = new RouteSaveReqBO();
        routeSaveReqBO.setUserId(userID);
        routeSaveReqBO.setUserCode(userCode);
        routeSaveReqBO.setIp(ip);
        routeSaveReqBO.setDubboPort(dubboPort);
        routeSaveReqBO.setWsSessionId(session.getId());
        routeSaveReqBO.setBusinessScene(businessScene);

        routeInfoRpcInvokeService.saveRouteInfo(routeSaveReqBO);
        // 保存session
        WebSocketSessionManager.saveSession(session);
    }

    public void removeRouteAndSession(WebSocketSession session)  {
        if (session == null){
            return;
        }
        String sessionId = session.getId();
        // 删除session
        WebSocketSessionManager.removeSession(session);
        // 删除路由
        Long userID = WebSocketSessionManager.getSessionAttribute(session, Constant.WsParamConstant.USER_ID, Long.class);
        String businessScene = WebSocketSessionManager.getSessionAttribute(session, Constant.WsParamConstant.BUSINESS_SCENE, String.class);
        RouteDeleteReqBO routeDeleteReqBO = new RouteDeleteReqBO();
        routeDeleteReqBO.setUserId(userID);
        routeDeleteReqBO.setWsSessionId(sessionId);
        routeDeleteReqBO.setBusinessScene(businessScene);
        routeInfoRpcInvokeService.deleteRouteInfo(routeDeleteReqBO);
    }


    public RouteGetRespBO getRouteInfo(String businessScene, Long userId){
        RouteGetReqBO routeGetReqBO = new RouteGetReqBO();
        routeGetReqBO.setUserId(userId);
        routeGetReqBO.setBusinessScene(businessScene);
        RouteGetRespBO routeInfo = routeInfoRpcInvokeService.getRouteInfo(routeGetReqBO);
        return routeInfo;
    }

    public String getCurrentIP(){
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            log.info("[RouteManager] 获取当前IP异常! ", e);
        }
        return null;
    }
}
